// JavaScript Document
$(document).ready(function () {
						
	jQuery.each(jQuery.browser, function(i) {	
		if($.browser.msie){
				$('#close_reply').bind('click', function(){
					$('#postbox').animate({height: '400px'}, 500 );								  
					$('#postboxshade').animate({height: '422px'}, 500 )	;
					$('#reply_box').hide();
					$('.ui-overlay').hide();
					$("#reply_box_active").val(0)
				});	
		} else {
			if($("#reply_box_active").val()==1){
				$('#close_reply').bind('click', function(){
					$('#postbox').animate({height: '400px'}, 500 );								  
					$('#postboxshade').animate({height: '422px'}, 500 )	;
					$('#reply_box').animate({opacity: 0.0}, 500, 'linear', function(){ $(this).hide(); });
					$('.ui-overlay').animate({opacity: 0.0}, 500, 'linear', function(){ $(this).hide(); });
					$('#preview').empty();
					$("#reply_box_active").val(0);
					//$('#reply_box_container').hide();
				});	
			}
		}
	});
						
	//hover states on the static widgets
	$('#close_reply, #post_link, #preview_link').hover(
		function() { $(this).addClass('ui-state-hover'); }, 
		function() { $(this).removeClass('ui-state-hover'); }
	)

	$('.smiley').bind('click', function(){
		var smilryUrl = $(this).attr('alt');
		$('#reply_message_box').val($('#reply_message_box').val()+smilryUrl)								
	});
	
	$('.bbcode_button').bind('click', function(){
		var bbcode = $(this).attr('id');
		$('#reply_message_box').val($('#reply_message_box').val()+bbcode)								
	});
	
	$('#preview_link').bind('click', function(){
			$('#postbox').animate({height: '700px'}, 500 );								  
			$('#postboxshade').animate({height: '722px'}, 500 )	;
			
			var previewText = $('#reply_message_box').val();
			previewText = previewText.replace(/ /g, "&nbsp;")
			
			$.get('ajax/forum/parsepost.php', { text:previewText }, function(data){
				$('#preview').html(data);
			});
	});
	
	$('#post_link').bind('click', function(){										   
		$('#preview').empty();
		$('#postbox').animate({height: '400px'}, 500 );								  
		$('#postboxshade').animate({height: '422px'}, 500 )	;
		
		var messageText = $('#reply_message_box').val();
		messageText = messageText.replace(/ /g, "&nbsp;")
		var titleText = $('#reply_title_box').val();
		if($('#sticky:checked').length==1){
			checked = "stick"
		} else {
			checked = "dontstick"
		}
		if(messageMode=="reply"){
			parseId = $('#topic_id').attr('title');
		} else if(messageMode=="newtopic"){
			parseId = $('#board_id').attr('title');
		} else if(messageMode=="edit"){
			parseId = editId;
		} else {
			return;
		}
		catId = $('#category_id').attr('title');
		userId = $('#post'+editId).attr('dd');
		$.get('ajax/forum/postreply.php', { title: titleText, message:messageText, type: messageMode, id: parseId, categoryId: catId, uid: userId, sticky: checked, quote: quoteId }, function(data){
			//alert(data);
			if(data.length==0){
				fetchPage('topic', $('#topic_id').attr('title'))
				$('#reply_box_container').empty();
				$('#reply_box_container').hide();
				$("#reply_box_active").val(0)
			} else {
				
				if(data=="newtopic"){
					fetchPage('board', $('#board_id').attr('title'))
					$('#reply_box_container').empty();
					$('#reply_box_container').hide();
					$("#reply_box_active").val(0);
				} else {
					$('#postbox').animate({height: '460px'}, 500 );								  
					$('#postboxshade').animate({height: '482px'}, 500, 'linear', function(){
						$('#preview').css('opacity', 0.0);														  
						$('#preview').html('<div id="overall_err" style="margin-top:10px;"><div class="ui-widget"><div class="ui-state-error ui-corner-all" style="padding: 0 .7em;"><p>'+data+'</p></div></div></div'); 
						$('#preview').animate({opacity: 1.0}, 500 );														  

					})	;
				}
			}
		});			
	});


});